#! /bin/bash

# 配置docker公司私有仓库hosts和docker daemon.json配置文件
install_docker() {
        echo "10.0.0.100	reg.fastplusplus.cc" >>/etc/hosts
        echo "hosts文件配置完成"
        touch /etc/docker/daemon.json
        echo -e "{
          \"debug\": true,
          \"experimental\": false,
          \"insecure-registries\": [
           \"reg.fastplusplus.cc\",
            \"10.0.0.100\",
            \"10.0.0.131:8002\"
          ]
        }" >>/etc/docker/daemon.json
        systemctl restart docker.service
        echo "配置daemon.json完成"
}
function zero_node() {
        # 刪除docker容器
        docker rm -f control_platform
        echo "删除节点容器成功"
        # 获取docker镜像id
        res=$(docker images | grep control_platform | awk '{print $3}')
        echo "删除镜像id为：${res}"
        docker rmi control_platform:beta
        echo "删除镜像tag成功"
        docker rmi ${res}
        echo "删除镜像成功,开始下载镜像"

        docker pull $1
        echo "下载镜像完成"
        # 修改刚下载的镜像tag
        imagesId=$(docker images | grep frontend_node | awk '{print $3}')
        docker tag $imagesId control_platform:beta
        docker run -itd -p 1234:1234 --restart=always --name control_platform control_platform:beta
        echo "更新节点前端完成"
}

zero_cluster() {
        # 刪除docker容器
        docker rm -f cluster_platform
        echo "删除集群容器成功"
        # 获取docker镜像id
        res=$(docker images | grep cluster_platform | awk '{print $3}')
        echo "删除镜像id为：${res}"
        docker rmi cluster_platform:beta
        echo "删除镜像tag成功"
        docker rmi ${res}
        echo "删除镜像成功，开始下载镜像"

        docker pull $1
        echo "下载镜像完成"
        # 修改刚下载的镜像tag
        imagesId=$(docker images | grep frontend_cluster | awk '{print $3}')
        docker tag $imagesId cluster_platform:beta
        docker run -itd -p 1235:1235 --restart=always --name cluster_platform cluster_platform:beta
        echo "更新集群前端完成"
}

case $1 in
"i" | "install")
        install_docker
        ;;
"update_node")
        con=$4
        zero_node $con
        ;;
"update_cluster")
        con=$4
        zero_cluster $con
        ;;
esac
